﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MonoGame;
using Microsoft.Xna.Framework;

namespace TunnelsOfDeath.Objects
{
    /// <summary>
    /// Based on sprite class, represtens single object on gamemap, that can fall down if area beneath is clear.
    /// </summary>
    class FallingDown : Sprite
    {

        public FallingDown(
            int _textureID,
            Dictionary<Direction, Vector2> _animationTable,
            Dictionary<Direction,int> _animationFrames,
            ObjectType _type,
            Vector2 _position,
            double _dealy): base (_textureID,_animationTable,_animationFrames,_type,_position,_dealy)
        {
            
        }

        public override void UpdatePosition(double _tag)
        {

            if (!CheckTag(_tag))
            {


                Sprite objectBelow = Globals.Map[(int)position.X, (int)position.Y + 1];
                if (objectBelow == null)
                {
                    ObjectDirection = Direction.down;
                    MoveTo(new Vector2((int)position.X, (int)position.Y + 1));
                    return;

                }

                if ((objectBelow.Type == ObjectType.rock || objectBelow.Type == ObjectType.crystal || objectBelow.Type == ObjectType.wall || objectBelow.Type == ObjectType.door))
                {
                    List<Vector2> possibleLocations = new List<Vector2>();

                    if (Globals.Map[(int)position.X - 1, (int)position.Y + 1] == null && Globals.Map[(int)position.X - 1, (int)position.Y] == null)
                    {

                        possibleLocations.Add(new Vector2((int)position.X - 1, (int)position.Y));

                    }

                    if (Globals.Map[(int)position.X + 1, (int)position.Y + 1] == null && Globals.Map[(int)position.X + 1, (int)position.Y] == null)
                    {
                        possibleLocations.Add(new Vector2((int)position.X + 1, (int)position.Y));
                    }

                    if (possibleLocations.Count > 1)
                    {
                        ObjectDirection = Direction.down;
                        if (Globals.random.NextDouble() > 0.5f)
                        {
                            MoveTo(possibleLocations[0]);
                        }
                        else
                        {
                            MoveTo(possibleLocations[1]);
                        }
                    }
                    else if (possibleLocations.Count == 1)
                    {
                        ObjectDirection = Direction.down;
                        MoveTo(possibleLocations[0]);
                    } else if (possibleLocations.Count == 0)
                    {
                        Globals.PositionUpdate.Remove(this);
                        ObjectDirection = Direction.stopped;
                    }

                }
                else
                {
                    
                    Collisions.HandleCollision(this, objectBelow);
                    Globals.PositionUpdate.Remove(this);
                    ObjectDirection = Direction.stopped;
                }
            }
           
        }

        public override void MoveTo(Vector2 _location)
        {
            base.MoveTo(_location);
            Sprite objectBelow = Globals.Map[(int)position.X, (int)position.Y + 1];

            /*if (objectBelow != null)
            {
                if (objectBelow.ObjectDirection != Direction.stopped)
                {
                    ObjectDirection = Direction.stopped;
                }
            }*/
        }

        

    }
}
